#include "CaptureThread.h"

extern IplImage *cvFrame;

CaptureThread::CaptureThread(QObject *parent) : QThread(parent)
{
    frameCount = 0;

    d = dc1394_new();   // initialize libdc1394

    if(!d)
        qDebug()<< " CaptureThread(): libdc1394 not initialized";

    err = dc1394_camera_enumerate(d, &list);    // find cameras
    //DC1394_ERR_RTN(err, "Failed to enumerate cameras");\

    if(list->num=0) {
        dc1394_log_error("No cameras found");   // verify we have at least one camera
    }

    camera = dc1394_camera_new(d, list->ids[0].guid);   // work with first camera
    if(!camera) {
        dc1394_log_error("Failed to initialize camera with guid %llx", list->ids[0].guid);
    }

    dc1394_camera_free_list (list);

    err = dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT); // setup capture

    err = dc1394_video_set_transmission(camera, DC1394_ON); // start transmission



   dc1394video_modes_t *video_modes;
   //err = dc1394_video_get_supported_modes(camera, video_modes);

      /*
    err = dc1394_video_set_mode(camera, DC_);
    qDebug()<<err<< " mode "<< video_modes->modes[0];
    uint32_t h;
    uint32_t w;
    dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_320x240_YUV422, &w, &h);
    dc1394color_coding_t *color_coding;
    //dc1394_format7_get_color_coding(camera, video_modes->modes[3], color_coding);
    qDebug()<< "width "<<w<<"height "<<h << "Color coding ";
*/
}

void CaptureThread::run()
{
    uint32_t h;
    uint32_t w;
    dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0, &w, &h); // getting size of frame
    cvFrame = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U , 3); // initializing cv image frame

    dc1394framerate_t frame_rate;
    dc1394_video_get_framerate(camera, &frame_rate);
    qDebug("Current frame rate: %u",frame_rate );

    while(1) {



        frameCount++; // incrementing frame counter
        err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &framedc); // capture
        if(err!=0) qDebug()<<"Capture dequeue error "<<err;

        if(dc1394_capture_is_frame_corrupt(camera, framedc)==DC1394_TRUE) {
            qDebug()<< "Error";
        }

       // qDebug()<<"Frame size "<<(framedc->image) << "Frame no "<<frameCount<<framedc->color_coding<< "frame depth "<<framedc->data_depth;

        if(dc1394_convert_to_RGB8(framedc->image,  (uint8_t *)cvFrame->imageData, cvFrame->width,cvFrame->height, framedc->yuv_byte_order, DC1394_COLOR_CODING_YUV422, 16)!=DC1394_SUCCESS) {
            qDebug()<< "CaptureThread(): error converting frame from camera";
        } else {
            //cvCvtColor(cvFrame, cvFrame, CV_RGB2GRAY);
           //cvShowImage("Dc image window", cvFrame);
        }
        err = dc1394_capture_enqueue(camera, framedc); // release the buffer
    }
}

void CaptureThread::close()
{
    err = dc1394_video_set_transmission(camera, DC1394_OFF); // stop transmission

    err = dc1394_capture_stop(camera); // stop capture

    dc1394_camera_free(camera); // cleanup and exit
    dc1394_free(d);

}
